package com.chunlin.servlet.cookieandseesion;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/setCookie")
public class SetCookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //期望通过doGet方法,把一个自定义的cookie数据返回到浏览器里面
        Cookie cookie1 = new Cookie("date", "2024-08-01");
        //访问setCookie请求的时候,代码会构造Cookie放到响应中来
        resp.addCookie(cookie1);
        Cookie cookie2 = new Cookie("time", "16:00");
        resp.addCookie(cookie2);
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write("setCookie ok");
    }
}
